package com.ipet.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ipet.system.pojo.SysRole;
import com.ipet.system.pojo.UserRole;
import com.ipet.system.service.SysRoleService;
import com.ipet.system.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userRole")
@Api("管理员角色管理")
public class UserRoleController {
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private SysRoleService roleService;
    @ApiOperation(value = "给用户添加角色")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id"  , name = "id" , required = true , dataType = "Integer" , paramType = "query"),
            @ApiImplicitParam(value = "要添加的角色"  , name = "roleName" , required = true , dataType = "String" , paramType = "query")

    })
    @PostMapping("/addRole")
    public ResponseEntity<Map<String , Object>> saveOrUpdate(Integer id , String roleName){
        SysRole sysRole = roleService.getOne(new QueryWrapper<SysRole>().eq("privilege", roleName));
        Map<String , Object> map = new HashMap<>();
        if(sysRole != null){
            UserRole userRole = new UserRole();
            userRole.setRId(sysRole.getId());
            userRole.setSId(id);
            List<UserRole> list = userRoleService.list(new QueryWrapper<UserRole>().eq("s_id", id).eq("r_id", sysRole.getId()));
            if(list.size() != 0){
                map.put("message" , "此管理员已拥有此角色！");
            }else{
                boolean saveOrUpdate = userRoleService.saveOrUpdate(userRole);
                map.put("saveOrUpdate" , saveOrUpdate);
            }

            return ResponseEntity.ok(map);
        }
        return ResponseEntity.notFound().build();
    }
    @ApiOperation(value = "给用户删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id"  , name = "uId" , required = true , dataType = "Integer" , paramType = "query"),
            @ApiImplicitParam(value = "要删除的角色"  , name = "roleName" , required = true , dataType = "String" , paramType = "query")

    })
    @PostMapping("/deleteRole")
    public ResponseEntity<Boolean> deleteRole(Integer uId , String roleName){
        SysRole sysRole = roleService.getOne(new QueryWrapper<SysRole>().eq("privilege", roleName));
        if(sysRole != null){
            boolean remove = userRoleService.remove(new QueryWrapper<UserRole>().eq("s_id", uId).eq("r_id", sysRole.getId()));
            return ResponseEntity.ok(remove);
        }
        return ResponseEntity.notFound().build();
    }
}
